home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / sched.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  5KB  |  134 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''A generally useful event scheduler class.
  5.  
  6. Each instance of this class manages its own queue.
  7. No multi-threading is implied; you are supposed to hack that
  8. yourself, or use a single instance per application.
  9.  
  10. Each instance is parametrized with two functions, one that is
  11. supposed to return the current time, one that is supposed to
  12. implement a delay.  You can implement real-time scheduling by
  13. substituting time and sleep from built-in module time, or you can
  14. implement simulated time by writing your own functions.  This can
  15. also be used to integrate scheduling with STDWIN events; the delay
  16. function is allowed to modify the queue.  Time can be expressed as
  17. integers or floating point numbers, as long as it is consistent.
  18.  
  19. Events are specified by tuples (time, priority, action, argument).
  20. As in UNIX, lower priority numbers mean higher priority; in this
  21. way the queue can be maintained as a priority queue.  Execution of the
  22. event means calling the action function, passing it the argument
  23. sequence in "argument" (remember that in Python, multiple function
  24. arguments are be packed in a sequence).
  25. The action function may be an instance method so it
  26. has another way to reference private data (besides global variables).
  27. '''
  28. import heapq
  29. from collections import namedtuple
  30. __all__ = [
  31.     'scheduler']
  32. Event = namedtuple('Event', 'time, priority, action, argument')
  33.  
  34. class scheduler:
  35.     
  36.     def __init__(self, timefunc, delayfunc):
  37.         '''Initialize a new instance, passing the time and delay
  38.         functions'''
  39.         self._queue = []
  40.         self.timefunc = timefunc
  41.         self.delayfunc = delayfunc
  42.  
  43.     
  44.     def enterabs(self, time, priority, action, argument):
  45.         '''Enter a new event in the queue at an absolute time.
  46.  
  47.         Returns an ID for the event which can be used to remove it,
  48.         if necessary.
  49.  
  50.         '''
  51.         event = Event(time, priority, action, argument)
  52.         heapq.heappush(self._queue, event)
  53.         return event
  54.  
  55.     
  56.     def enter(self, delay, priority, action, argument):
  57.         '''A variant that specifies the time as a relative time.
  58.  
  59.         This is actually the more commonly used interface.
  60.  
  61.         '''
  62.         time = self.timefunc() + delay
  63.         return self.enterabs(time, priority, action, argument)
  64.  
  65.     
  66.     def cancel(self, event):
  67.         '''Remove an event from the queue.
  68.  
  69.         This must be presented the ID as returned by enter().
  70.         If the event is not in the queue, this raises ValueError.
  71.  
  72.         '''
  73.         self._queue.remove(event)
  74.         heapq.heapify(self._queue)
  75.  
  76.     
  77.     def empty(self):
  78.         '''Check whether the queue is empty.'''
  79.         return not (self._queue)
  80.  
  81.     
  82.     def run(self):
  83.         """Execute events until the queue is empty.
  84.  
  85.         When there is a positive delay until the first event, the
  86.         delay function is called and the event is left in the queue;
  87.         otherwise, the event is removed from the queue and executed
  88.         (its action function is called, passing it the argument).  If
  89.         the delay function returns prematurely, it is simply
  90.         restarted.
  91.  
  92.         It is legal for both the delay function and the action
  93.         function to to modify the queue or to raise an exception;
  94.         exceptions are not caught but the scheduler's state remains
  95.         well-defined so run() may be called again.
  96.  
  97.         A questionable hack is added to allow other threads to run:
  98.         just after an event is executed, a delay of 0 is executed, to
  99.         avoid monopolizing the CPU when other threads are also
  100.         runnable.
  101.  
  102.         """
  103.         q = self._queue
  104.         delayfunc = self.delayfunc
  105.         timefunc = self.timefunc
  106.         pop = heapq.heappop
  107.         while q:
  108.             (time, priority, action, argument) = checked_event = q[0]
  109.             now = timefunc()
  110.             if now < time:
  111.                 delayfunc(time - now)
  112.                 continue
  113.             event = pop(q)
  114.             if event is checked_event:
  115.                 action(*argument)
  116.                 delayfunc(0)
  117.                 continue
  118.             heapq.heappush(q, event)
  119.  
  120.     
  121.     def queue(self):
  122.         '''An ordered list of upcoming events.
  123.  
  124.         Events are named tuples with fields for:
  125.             time, priority, action, arguments
  126.  
  127.         '''
  128.         events = self._queue[:]
  129.         return map(heapq.heappop, [
  130.             events] * len(events))
  131.  
  132.     queue = property(queue)
  133.  
  134.